<?php

namespace app\admin\controller\task;

use app\common\controller\Backend;
use think\Db;

/**
 * 订单管理
 *
 * @icon fa fa-circle-o
 */
class PaymentOrder extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\shop\Order
     */
    protected $model = null;

    /**
     * 快速搜索时执行查找的字段
     */
    //protected $searchFields = 'order_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\record\Money;
        $this->view->assign("payedList", $this->model->getPayedList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index(){
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            $_where["a.payed"] = 2;
            if(session('admin')['athur_id']==1) {
                $_where['t.shop_admin_id'] = session('admin')['athor'];
            }else{
                $_where['t.shop_admin_id'] =session('admin')['id'];
            }
            $join[] = ["sd_shop t","t.shop_id = a.shop_id","left"];
            $join[] = ["sd_admin n","n.id = t.shop_admin_id","left"];
            $join[] = ["sd_shop_order","sd_shop_order.order_id = a.order_id","left"];
            $field = "a.record_id,a.order_id,a.username,a.bank_name,a.on_time,a.bank_no,a.record_id as id,n.bank_account as nbank_account";
            $field .= ",sd_shop_order.order_id,sd_shop_order.order_sno,sd_shop_order.shop_name,sd_shop_order.tao_bao_no,sd_shop_order.real_money,sd_shop_order.create_time,sd_shop_order.status";
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->alias("a")
                ->join($join)
//                ->field("a.*,")
                ->where($where)
                ->where($_where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->alias("a")
                ->join($join)
                //->with('order')
                ->field($field)
                ->where($where)
                ->where($_where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            //echo $this->model->getLastSql();exit;
            $list = collection($list)->toArray();

            foreach($list as &$value){

                $value["sd_shop_order.order_sno"] = $value["order_sno"];
                $value["sd_shop_order.shop_name"] = $value["shop_name"];
                $value["sd_shop_order.tao_bao_no"] = $value["tao_bao_no"];
                $value["sd_shop_order.real_money"] = $value["real_money"];
                $value["sd_shop_order.create_time"] = $value["create_time"];
                $value["a.on_time"] = $value["on_time"];
                $value["sd_shop_order.status"] = $value["status"];

            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }





    /**
     *批量催付
     *
     * 催付
     */
    public function expedit(){

        if ($this->request->isPost()) {
            $params = $this->request->post();
            if ($params)
            {
                //halt($params["ids"]);
                $row = $params["row"];
                //$status = $row["status"];
                if($row["ids"]){//订单id,单个催付

                    $_where["order_id"] = $row["ids"];
                    $_where["payed"] = 2;
                    $_where["status"] = 5;
                    $order_info = Db::name("shop_order")->where($_where)->find();

                    if(!$order_info){
                        renderJson("该订单信息不能催付！有误的话请联系技术人员",0);
                    }

                    //发送短信模板订单完成
                    $uid = $order_info["uid"];
                    $openid = Db::name("wx_users")->where("uid",$uid)->value("openid");
                    $user_name = Db::name("user")->where("uid",$uid)->value("username");
                    $time = date("Y-m-d H:i:s",time());
                    $content = [
                        $user_name,$order_info["goods_sno"],$time
                    ];//推荐内容
                    $msg = [
                        "condition"=>13,
                        "touser"=>$openid,
                        "content"=>$content
                    ];
                    $MiniMsg = new  \app\common\lib\MiniMsg($msg);
                    $MiniMsg->sendMsg();//发送短信模板订单已完成

                    renderJson("催款成功",1);

                }

                if($params["ids"]){//批量发送

                    $count = 0;
                    //获取所有的金额
                    foreach ($params["ids"] as $key=>$value){
                        //halt($value["order"]["status"]);
                        if($value["order"]["status"]==5) {
                            //发送短信模板订单完成
                            $uid = $value["order"]["uid"];
                            $openid = Db::name("wx_users")->where("uid", $uid)->value("openid");
                            $user_name = Db::name("user")->where("uid", $uid)->value("username");
                            $time = date("Y-m-d H:i:s", time());
                            $content = [
                                $user_name, $value["order"]["goods_sno"], $time
                            ];//推荐内容
                            $msg = [
                                "condition" => 13,
                                "touser" => $openid,
                                "content" => $content
                            ];
                            $MiniMsg = new  \app\common\lib\MiniMsg($msg);
                            $MiniMsg->sendMsg();//发送短信模板订单已完成
                            $count++;

                        }

                    }

                    if($count>0) {

                        renderJson("催款成功",1);

                    }else{

                        renderJson("您选择的没有在状态的！",1);
                    }


                }

            }
        }


    }








}
